import { readFileSync } from 'fs';
/*当前时间按规则拼接后返回*/
function getTimeFormat() {
  const date = new Date();
  let month = (date.getMonth() + 1)
  if (month < 10) {
    month = "0" + month
  }
  let day = date.getDate()
  if (day < 10) {
    day = "0" + day
  }
  let hour = date.getHours()
  if (hour < 10) {
    hour = "0" + hour
  }
  let min = date.getMinutes()
  if (min < 10) {
    min = "0" + min
  }
  let second = date.getSeconds()
  if (second < 10) {
    second = "0" + second
  }
  let timeStr = month + "" + day + "_" + hour + "" + min + "" + second
  return timeStr
}

export function getLocalFileContent(): Object {
  const path = require('path');

  //获取当前hvigorfileConfig.ts的父目录(因为hvigorfileConfig.ts和local.properties同级)
  const parentDir = path.dirname(__filename);
  /*获取local.properties文件内容*/
  const data = readFileSync(path.join(parentDir, "local.properties"), 'utf8')
  const lines = data.split("\n")

  for (let i = 0; i < lines.length; i++) {
    const item = lines[i]
    /*忽略#开头的内容*/
    if (item.startsWith("#")) {
      continue
    }
    /*过滤无用数据*/
    if (!item||item.length<=1) {
      continue
    }
    let eachLine = item.split("=")
    /*获取配置内容*/
    const key = eachLine[0].trim()
    const value = eachLine[1].trim()

    // console.info(key+"==="+value)
    if (!isNaN(value)) {
      config[key] = Number(value.trim())
    } else {
      config[key] = value.trim()
    }
  }
  config["buildTime"] = getTimeFormat()
  /*将local.properties文件中的配置内容和当前时间保存至config中*/
  return config
}

/*用于保存打包配置(local.properties文件中的内容)*/
let config = new Object()
export { config }